#ifndef _BASE_CARD_H_
#define _BASE_CARD_H_

#include <string>
#include <stdio.h>

class Card
{
public:
    enum
    {
        E_Max_Count = 54,
        E_Min_Point = 1,
        E_Max_Point = 0x0D,
        E_Invalid_Card = 0xFF,
    };
    
	enum ECardType
	{
		E_Card_Fangkuai = 0,
		E_Card_Meihua   = 1,
		E_Card_Hongtao  = 2,
		E_Card_Heitao   = 3,
		E_Card_King     = 4, 
		E_CardType_Count,
	};

	static bool IsValid(unsigned char card)
	{
        unsigned char type = CardType(card);
        unsigned char val = CardPoint(card);
        if( type < E_CardType_Count && val >= E_Min_Point && val <= E_Max_Point )
        {
            if( type == E_Card_King && val > 2 )
            {
                return false;
            }
            return true;
        }
        return false;
	}

	static unsigned char CardPoint(unsigned char card)
	{
		return (unsigned char)(card & 0xF);
	}

    static unsigned char CardType(unsigned char card)
    {
		return (unsigned char)((card>>4) & 0xF);
    }

	static unsigned char BuildCard(unsigned char type, unsigned char point)
	{
        unsigned char v = (type << 4) | point;
        return v;
	}

	static std::string StrFormat(unsigned char card)
	{
		unsigned char t = CardType( card );
		unsigned char v = CardPoint( card );
		std::string ret("");
		switch( t )
		{
		case E_Card_Heitao:
			ret = "黑桃";
			break;
		case E_Card_Hongtao:
			ret = "红桃";
			break;
		case E_Card_Meihua:
			ret = "梅花";
			break;
		case E_Card_Fangkuai:
			ret = "方块";
			break;
		case E_Card_King:
			{
				if( v == 1 )
					return std::string("小王");
				else
					return std::string("大王");
			}
		default:
			break;
		}
		switch( v )
		{
		case 1:
			ret += "A";
			break;
		case 11:
			ret += "J";
			break;
		case 12:
			ret += "Q";
			break;
		case 13:
			ret += "K";
			break;
		default:
			{
				char strVal[16] = {0};
				sprintf( strVal, "%d", (int)v );
				ret += strVal;
			} break;
		};
		return ret;
	}
};

#endif

